# !/usr/bin/env python
# -*- coding: utf-8 -*-
"""
@Time        : 2021/9/18 19:40
@Author      : Albert Darren
@Contact     : 2563491540@qq.com
@File        : simple_linear-regression.py
@Version     : Version 1.0.0
@Description : TODO
@Created By  : PyCharm
"""
import numpy as np


def simple_linear_regression(x: np.ndarray, y: np.ndarray):
    """
    实现简单线性回归，返回参数a,b
    :param x: 特征向量
    :param y: 标记向量
    :return: 简单线性回归参数a,b
    """
    a = (x - x.mean()).dot(y - y.mean()) / np.sum((x - x.mean()) ** 2)
    b = y.mean() - a * x.mean()
    return a, b


feature_vector = np.array([2018, 2019, 2020, 2021, 2022])
target_vector = np.array([151, 174, 217, 299, 312])
print(simple_linear_regression(feature_vector,target_vector))